<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html debug="true">
<head>

<script type="text/javascript" src="../../lib/jay.js"></script>

</head>
<body>

<script type="text/javascript">
  
  var game = new Jay.Game({
    screen: { backgroundStyle:"#009900"},
    input:  { up:"KEY_UP", down:"KEY_DOWN", left:"KEY_LEFT", right:"KEY_RIGHT" }
  });
  
  var sheepSprite = game.loadSprite( '../images/sheep.gif', {
      defaultFrameWidth: 20,
      defaultFrameHeight: 20,
      animations: {
        walk_r:  [{x:0,y:0}, {x:0,y:20}, {x:0,y:0}, {x:0,y:40}],
        walk_ru: [{x:20,y:0}, {x:20,y:20}, {x:20,y:0}, {x:20,y:40}],
        walk_u:  [{x:40,y:0}, {x:40,y:20}, {x:40,y:0}, {x:40,y:40}],
        walk_lu: [{x:60,y:0}, {x:60,y:20}, {x:60,y:0}, {x:60,y:40}],
        walk_l:  [{x:80,y:0}, {x:80,y:20}, {x:80,y:0}, {x:80,y:40}],
        walk_ld: [{x:100,y:0}, {x:100,y:20}, {x:100,y:0}, {x:100,y:40}],
        walk_d:  [{x:120,y:0}, {x:120,y:20}, {x:120,y:0}, {x:120,y:40}],
        walk_rd: [{x:140,y:0}, {x:140,y:20}, {x:140,y:0}, {x:140,y:40}],
        idle_r:  [{x:0,y:0}],
        idle_ru: [{x:20,y:0}],
        idle_u:  [{x:40,y:0}],
        idle_lu: [{x:60,y:0}],
        idle_l:  [{x:80,y:0}],
        idle_ld: [{x:100,y:0}],
        idle_d:  [{x:120,y:0}],
        idle_rd: [{x:140,y:0}]
      }
    });

  var Sheep = game.createActorClass(Jay.Actor, {
    init: function(x, y, sprite) {
      this.orientation = "r";
      this._super({ sprite: sprite, x: x, y: y, animation: sheepSprite.animations.idle_r });
    },
    run: function () {
      var speed = 2;

      var actions = game.input.actions;
      var velX = (actions.right.isDown ? speed : 0 ) + (actions.left.isDown ? -speed : 0);
      var velY = (actions.up.isDown ? -speed : 0) + (actions.down.isDown ? speed : 0);
      
      if(velX==0 && velY==0) {
        this.setAnimation(this.sprite.animations['idle_' + this.orientation]);
      } else {
        this.orientation = (velX<0 ? "l" : (velX>0 ? "r" : "")) + 
                           (velY<0 ? "u" : (velY>0 ? "d" : ""));
        this.increaseXY(velX, velY);
        this.setAnimation(this.sprite.animations['walk_' + this.orientation]);
      }
      this.animate();
    }
  });
  
  game.loaded = function() {
    var s = new Sheep(200, 200, sheepSprite);
    this.start();
  };
  
  game.waitForLoad();


</script>

</body>
</html>

